#include "ResumeModuleState.h"
#include "MainLoopState.h"
#include "Portfolio.h"
#include <fstream>

ResumeModuleState::ResumeModuleState(string name) : ModuleState(name)
{
}

ResumeModuleState::~ResumeModuleState(void)
{
}

void ResumeModuleState::enter()
{
	state = 2;
	//Reset Mouse Wheel
	position_mouse_z(0);
	scare_mouse();

	//Generate Text for Game
	dataFileName = "PORT/RESUME/" + name + "_G.dat";

	//Load Graphic file for the game
	port->getRMgr()->loadDataFile(dataFileName);

	//Mouse hit detection image
	submenubutton_pmask = create_allegro_pmask(
		(BITMAP *)port->getRMgr()->GraphicPackA[Submenubuttonpmask].dat);
	B_mousehit = create_bitmap(1,1);
	mousehit_pmask = create_allegro_pmask(B_mousehit);

	port->getGUtil()->clearBuffers();
}

void ResumeModuleState::leave()
{
	destroy_pmask(mousehit_pmask);
	destroy_pmask(submenubutton_pmask);
	release_bitmap(B_mousehit);

	port->getRMgr()->unloadDataFile(dataFileName);
	clear_keybuf();

	port->getGUtil()->clearBuffers();
}

void ResumeModuleState::update(const float delta)
{
	while(Portfolio::ticks > 0)
	{
		checkMouse();
		Portfolio::ticks --;
	}//End while(Portfolio::ticks > 0)


	if (quit)
	{
		port->changeState(new MainLoopState());
	}
}
void ResumeModuleState::render()
{
	port->getGUtil()->drawToBackBuffer(
		(BITMAP *)port->getRMgr()->GraphicPackA[Submenu].dat,
		0, 0);

	drawButtons(1);
	rect(port->getGUtil()->getLevel(0), 50, 110, 750, 510, C_ScrollBar);

	port->getGUtil()->blitToBackBuffer(
		(BITMAP *)port->getRMgr()->getData(0, dataFileName),
		0, scroll_position*50, 52, 110, 700, 400);

	port->getGUtil()->blitToBackBuffer(
		(BITMAP *)port->getRMgr()->getData(1, dataFileName),
		0,((scroll_position)*50) - 1106,52,110,700,400);

	int linetrip;
	int moused = 0;
	int dx = BUTA_X - mouse_x;
	int dy = BUTA_Y - mouse_y;

	//Draw the horizontal scroll bar.
	vline(port->getGUtil()->getLevel(0), BUTA_X  , BUTA_Y, BUTB_Y, C_ScrollBar);
	vline(port->getGUtil()->getLevel(0), BUTA_X+1, BUTA_Y, BUTB_Y, C_ScrollBar);
	vline(port->getGUtil()->getLevel(0), BUTA_X-1, BUTA_Y, BUTB_Y, C_ScrollBar);

	linetrip = (BUTB_Y - BUTA_Y)/35;

	vline(port->getGUtil()->getLevel(0), 
		BUTA_X,
		BUTA_Y + linetrip * scroll_position,
		BUTA_Y + linetrip * scroll_position + linetrip,
		C_ScrollBarSmall);

	//CALC DISTANCE
	moused = (dx*dx) + (dy*dy);
	triforce(
		BUTA_X,
		BUTA_Y,
		C_ScrollBarButton);

	//TEXT SCROLLER
	if(moused < 49)
	{
		triforce(
			BUTA_X, BUTA_Y,
			C_ScrollBarButtonHit,
			-10);

		if(mouse_b == 1 && mouselock == 0 && line > MAXLINE)
		{
			scroll_position--;
			if(scroll_position < 0)
			{
				scroll_position = 0;
			}
			mouselock = 2;
		}
	}

	dx = BUTB_X - mouse_x;
	dy = BUTB_Y - mouse_y;

	//CALC DISTANCE
	moused = (dx*dx) + (dy*dy);
	triforce(
		BUTA_X, BUTB_Y,
		C_ScrollBarButton, -10);

	if(moused < 49)
	{
		triforce(
			BUTA_X,
			BUTB_Y,
			C_ScrollBarButtonHit,
			-10);

		if(mouse_b == 1 && mouselock == 0)
		{
			scroll_position++;
			if(scroll_position > 35)
			{
				scroll_position = 35;
			}
			mouselock = 2;
		}
	}

	//WHEEL MOUSE
	if(mouse_z < mouseoldz || key[KEY_DOWN])
	{
		scroll_position++;
		if(scroll_position > 35)
		{
			scroll_position = 35;
		}
		mouseoldz = mouse_z;
	}
	if(mouse_z > mouseoldz || key[KEY_UP])
	{
		scroll_position--;
		if(scroll_position < 0)
		{
			scroll_position = 0;
		}
		mouseoldz = mouse_z;
	}

	port->getGUtil()->drawTextToBackBuffer(
		port->getRMgr()->font,
		defaultmsg[9],
		90,490,
		C_SetsumeiText);


	port->getGUtil()->drawTextToBackBuffer(
		port->getRMgr()->font,
		defaultmsg[9],
		390,530,
		C_ScrollBarSmall);

	//Mouse pointer dot
	rectfill(
		port->getGUtil()->getLevel(0),
		mouse_x - 3,
		mouse_y - 3,
		mouse_x + 3,
		mouse_y + 3,
		makecol(0,0,0));
	rectfill(
		port->getGUtil()->getLevel(0),
		mouse_x - 2,
		mouse_y - 2,
		mouse_x + 2,
		mouse_y + 2,
		makecol(255,255,255));

	port->getGUtil()->drawBackBufferToScreen();
	port->getGUtil()->clearBuffers();
	release_screen();
	// If we are quiting save the back buffer for the fade out
}